package com.cskaoyan.service.impl;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.service.StorageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文件（图片）上传保存相关业务接口实现类
 *
 * @author Chen_Feng
 * @since 2022/06/26 15:32
 */
@Service
@Transactional
public class StorageServiceImpl implements StorageService {

    @Value("${file.storage.origin}")
    String webAdress;

    @Autowired
    MarketStorageMapper marketStorageMapper;

    /**
     * 文件上传时提供的数据，将之保存到数据库，并获得自增主键id后将对象返回
     *
     * @param storage 上传的文件拥有的属性封装成的对象
     * @return com.cskaoyan.bean.MarketStorage
     * @author Chen_Feng
     * @since 2022/06/26 15:34
     */
    @Override
    public MarketStorage create(MarketStorage storage) {

        // 对数据库进行增加操作
        marketStorageMapper.insertStorage(storage);
        return storage;
    }

    /**
     * 对象存储
     * list
     * @param baseParam
     * @return
     * @author zhangsenqi
     */
    @Override
    public CommonData<MarketStorage> list(BaseParam baseParam, String key, String name) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria();

        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());

        criteria.andDeletedEqualTo(false);
        if (key != null && !"".equals(key)) {
            criteria.andKeyEqualTo(key);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(example);

        //对图片路径进行处理
        for (MarketStorage marketStorage : marketStorages) {
            String url = marketStorage.getUrl();
            if (!url.startsWith("http://")) {
                marketStorage.setUrl(webAdress + url);
            }
        }
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketStorages);

        return CommonData.data(pageInfo);
    }


    /**
     * 对象存储
     * update
     * @param marketStorage
     * @return
     * @author zhangsenqi
     */
    @Override
    public void update(MarketStorage marketStorage) {
        marketStorageMapper.updateByPrimaryKey(marketStorage);
    }


    /**
     * 对象存储
     * delete
     * @param marketStorage
     * @return
     * @author zhangsenqi
     */
    @Override
    public void delete(MarketStorage marketStorage) {
        MarketStorage marketStorage1 = new MarketStorage();
        marketStorage1.setId(marketStorage.getId());
        marketStorage1.setDeleted(true);

        marketStorageMapper.updateByPrimaryKeySelective(marketStorage1);
    }

}
